# -*- coding: utf-8 -*-
import pygame
from 精灵子类 import *

"""
系统参数
"""
# 屏幕宽度
SCREEN_WIDTH = 480
# 屏幕高度
SCREEN_HIGHT = 700
# 定义屏幕对象
SCREE_RECT = pygame.Rect(SCREEN_WIDTH, SCREEN_HIGHT, 0, 0)
# 帧率
FRAME = 120

# 英雄默认位置
HERO_PLACE_X_INIT = 50
HERO_PLACE_Y_INIT = 500
# 英雄宽高
HERO_PLACE_WIDTH_INIT = 102
HERO_PLACE_HIGHT_INIT = 126

"""
图片地
方便后续改配置方案
"""
# 图片背景地址
BACKGROUND_IMAGE = "./images/background.png"
# 英雄飞机
HERO_IMAGE_1 = "./images/me1.png"
HERO_IMAGE_2 = "./images/me2.png"

# 敌人飞机
ENEMY_IMAGE_1 = "./images/enemy1.png"
ENEMY_IMAGE_2 = "./images/enemy2.png"

ENEMY_CRASH_IMAGE_1 = "./images/enemy1_down{}.png"
ENEMY_CRASH_IMAGE_2 = "./images/enemy2_down{}.png"
HERO_CRASH_IMAGE = "./images/me_destroy_{}.png"


class PlanGame(object):

    def __init__(self):
        print("游戏初始化")

        # 1.创建游戏窗口
        self.scree = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HIGHT))
        # 2.创建游戏时钟
        self.clock = pygame.time.Clock()
        # 3.调用私有方法，创建精灵和精灵组
        self.__creat_sprites()

        # 4.设置定时器事件
        pygame.time.set_timer(CREATE_ENEMY_EVENT, random.randint(1, 1000))

    # 创建精灵组
    def __creat_sprites(self):

        # 背景
        background = BackgroundSprite(BACKGROUND_IMAGE)
        background2 = BackgroundSprite(BACKGROUND_IMAGE, -1)
        self.backgroundGroup = pygame.sprite.Group(background, background2)

        # 创建敌机精灵组
        self.enemyGroup = pygame.sprite.Group()

        # 创建英雄组
        self.hero = Hero(HERO_IMAGE_1)
        self.heroGroup = pygame.sprite.Group(self.hero)

    def start(self):
        print("游戏开始...")

        while True:
            pass
            # 1.设置刷新帧率
            self.clock.tick(FRAME)
            # 2.事件监听
            self.__event_handler()
            # 3.碰撞检测
            self.__check_collide()
            # 4.更新/绘制精灵
            self.__update_sprites()
            # 5.更新显示
            pygame.display.update()

    # 事件监听
    def __event_handler(self):

        # 获取键值监听
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d]:
            self.hero.set_speed(pygame.K_RIGHT)
        elif keys_pressed[pygame.K_LEFT] or keys_pressed[pygame.K_a]:
            self.hero.set_speed(pygame.K_LEFT)
        else:
            self.hero.set_speed(0)

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                self.__game_over()
            elif event.type == CREATE_ENEMY_EVENT:
                # print("敌机创建事件")
                # 创建精灵敌机
                enemy = Enemy(ENEMY_IMAGE_1)
                self.enemyGroup.add(enemy)
            elif event.type == HERO_FIRE_EVENT:  # 英雄发射子弹
                self.hero.fire()

    # 碰撞检测
    def __check_collide(self):
        # 英雄子弹 碰撞 敌人飞机
        pygame.sprite.groupcollide(self.enemyGroup, self.hero.heroBulletGroup, True, True)
        # 英雄飞机 碰撞 敌人飞机
        enemies = pygame.sprite.spritecollide(self.hero, self.enemyGroup, True)
        if len(enemies) > 0:
            self.hero.kill()

            # 覆盖更新【不然hero内方法依旧可以执行。敌机继续造。子弹还在飞。】
            # 原因不明。子弹精灵组是在hero内的。都kill了。居然还能继续。
            self.__update_sprites = object

            # 直接停下太奇怪，刷个背景出来
            self.backgroundGroup.update()
            self.backgroundGroup.draw(self.scree)

            # TODO 什么重新开始，计分板就算了。先不做了

    # 更新/绘制精灵组
    def __update_sprites(self):

        self.backgroundGroup.update()
        self.backgroundGroup.draw(self.scree)

        self.enemyGroup.update()
        self.enemyGroup.draw(self.scree)

        self.heroGroup.update()
        self.heroGroup.draw(self.scree)

        self.hero.heroBulletGroup.update()
        self.hero.heroBulletGroup.draw(self.scree)
        pass

    @staticmethod
    def __game_over():
        print("游戏结束")
        pygame.quit()
        exit()


if __name__ == '__main__':
    # 创建游戏
    game = PlanGame()

    # 启动游戏
    game.start()
